package com.rjwh.dingdong.client.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aishua.lib.utils.AsStrUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.community.CommunityPostMyMainPageActivity;
import com.rjwh.dingdong.client.activity.loginregist.ThirdPartyLoginActivity;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.bean.localbean.CommunityPostListBean;
import com.rjwh.dingdong.client.bean.localbean.TopicData;
import com.rjwh.dingdong.client.bean.localbean.TopicHeader;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.util.PicPathUtil;
import com.rjwh.dingdong.client.widget.MyGridView;
import com.rjwh.dingdong.client.widget.RoundImageView;
import com.wby.base.AdapterBase;

/**
 * 社区帖子列表
 *
 * @author 王小珂
 * @ClassName CommunityPostAdapter
 * @Description TODO(这里用一句话描述这个类的作用)
 * @date 2016-3-29 下午2:17:39
 * @history 1.YYYY-MM-DD author: description:
 */
public class CommunityPostAdapter extends AdapterBase<CommunityPostListBean> {

    private static final int ITEM_TYPE_MAX_COUNT = 2;
    private static final int TYPE_TOP = 0;
    private Context context;
    private LayoutInflater layoutInflater;
    private int screen;

    public CommunityPostAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width_screen = dm.widthPixels;
        screen = (int) Math.round(width_screen * 0.75);
    }


    @Override
    public int getItemViewType(int position) {
        return getItem(position).getType();
    }

    @Override
    public int getViewTypeCount() {
        return ITEM_TYPE_MAX_COUNT;
    }


    @Override
    public CommunityPostListBean getItem(int position) {
        return mList.get(position);
    }

    @Override
    protected View getExView(int position, View convertView, ViewGroup viewGroup) throws Exception {
        TopViewHolder topViewHolder;
        PostViewHolder postViewHolder;
        final int itemType = this.getItemViewType(position);
        if (itemType == TYPE_TOP) {
            // 第一种item
            if (convertView == null) {
                topViewHolder = new TopViewHolder();
                convertView = layoutInflater.inflate(R.layout.community_top_item, null, false);
                topViewHolder.item_top_title = (TextView) convertView.findViewById(R.id.item_post_top_title);
                convertView.setTag(topViewHolder);
            } else {
                topViewHolder = (TopViewHolder) convertView.getTag();
            }
            TopicHeader topicHeader = (TopicHeader) getItem(position).getObject();
            topViewHolder.item_top_title.setText(topicHeader.getTitle());

        } else {
            // 第二种item
            if (convertView == null) {
                postViewHolder = new PostViewHolder();
                convertView = layoutInflater.inflate(R.layout.community_post_item, null, false);
                postViewHolder.imgView = (RoundImageView) convertView.findViewById(R.id.item_post_head_img_view);
                postViewHolder.usernameTV = (TextView) convertView.findViewById(R.id.item_post_user_name);
                postViewHolder.userlevelImg = (ImageView) convertView.findViewById(R.id.item_post_user_level_img);
                postViewHolder.usernametagRoot = (LinearLayout) convertView.findViewById(R.id.item_post_user_name_tag_root_ll);
                postViewHolder.timeTV = (TextView) convertView.findViewById(R.id.item_post_send_time);
                postViewHolder.positionTV = (TextView) convertView.findViewById(R.id.item_post_position_tv);
                postViewHolder.contentTV = (TextView) convertView.findViewById(R.id.item_post_content);
                postViewHolder.picGV = (MyGridView) convertView.findViewById(R.id.item_post_pic_gridview);
                convertView.setTag(postViewHolder);
            } else {
                postViewHolder = (PostViewHolder) convertView.getTag();
            }

            final TopicData topicData = (TopicData) getItem(position).getObject();
            String userlevel = topicData.getUserlevel();
            if (!AsStrUtils.isEmpty(userlevel)) {
                setLevelIcon(postViewHolder.userlevelImg, Integer.parseInt(userlevel));
            }
//            List<TagConfig> config = topicData.getTagconfig();
//            // 设置nameTag
//            postViewHolder.usernametagRoot.removeAllViews();
//            if (config != null) {
//                for (int i = 0; i < config.size(); i++) {
//                    NameTagView nameTagView = new NameTagView(context);
//                    TagConfig topicConfig = config.get(i);
//                    nameTagView.setTag(topicConfig, R.drawable.topic_username_tag_shape, false);
//                    nameTagView.setPadding(4, 1, 4, 1);
//                    postViewHolder.usernametagRoot.addView(nameTagView);
//                }
//            }
            if (!AsStrUtils.isEmpty(topicData.getName())) {
                postViewHolder.usernameTV.setText(topicData.getName());
            }
            if (topicData.getAvatar() != null) {
                Glide.with(context).load(PicPathUtil.getPicUrl(topicData.getAvatar())).placeholder(R.drawable.icon_defult_avatar).error(R.drawable.icon_defult_avatar).dontAnimate().diskCacheStrategy(DiskCacheStrategy.ALL).into(postViewHolder.imgView);
            }

            postViewHolder.imgView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean isLogined = MyApplication.spUtil.getBoolPreferenceByParamName(context,
                            LocalConstant.SP_IS_LOGINED);
                    Intent intent;
                    if (isLogined) {
                        intent = new Intent(context, CommunityPostMyMainPageActivity.class);
                        intent.putExtra("userid", topicData.getUserid());
                    } else {
                        intent = new Intent(context, ThirdPartyLoginActivity.class);
                    }
                    context.startActivity(intent);
                }
            });


            if (!AsStrUtils.isEmpty(topicData.getTime())) {
                if (AsStrUtils.isEmpty(topicData.getRead()) || topicData.getRead().equals("0")) {
                    postViewHolder.timeTV.setText(topicData.getTime());
                } else {
                    postViewHolder.timeTV.setText(topicData.getTime() + "/浏览" + topicData.getRead());
                }
            }
            if (!AsStrUtils.isEmpty(topicData.getRegion())) {
                postViewHolder.positionTV.setText(topicData.getRegion());
            }

            if (!AsStrUtils.isEmpty(topicData.getTitle())) {
                postViewHolder.contentTV.setText(topicData.getTitle());
            }
            MyPicAdapter adapter = new MyPicAdapter(context, topicData.getImage(), null, screen);
            // 设置GridView不抢夺焦点
            postViewHolder.picGV.setClickable(false);
            postViewHolder.picGV.setPressed(false);
            postViewHolder.picGV.setEnabled(false);
            postViewHolder.picGV.setAdapter(adapter);
        }

        return convertView;
    }

    private void setLevelIcon(ImageView imageView, int level) {
        int id = 0;
        switch (level) {
            case 1:
                id = R.drawable.icon_level_v1;
                break;
            case 2:
                id = R.drawable.icon_level_v2;
                break;
            case 3:
                id = R.drawable.icon_level_v3;
                break;
            case 4:
                id = R.drawable.icon_level_v4;
                break;
            case 5:
                id = R.drawable.icon_level_v5;
                break;
            case 6:
                id = R.drawable.icon_level_v6;
                break;
            default:
                break;
        }
        imageView.setImageResource(id);
    }

    @Override
    protected void onReachBottom() {

    }

    class PostViewHolder {
        RoundImageView imgView;
        TextView usernameTV;
        LinearLayout usernametagRoot;
        TextView timeTV;
        TextView positionTV;
        TextView contentTV;
        MyGridView picGV;
        ImageView userlevelImg;
    }

    class TopViewHolder {
        TextView item_top_title;
    }

}
